#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import logging

# 创建Logger
logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
                    level=logging.INFO)
logger = logging.getLogger()

# 上传文件的路径
UPLOAD_URL = "http://upload.img.inner.snappea.com/image/em-video/upload"
# 图片路径的前缀
PIC_PREFIX = "http://img.mobiuspace.com/image/em-video/"


# 获取ip
def get_ip():
    url = 'http://internal-ecs-prod-alb-04-1868794252.us-east-1.elb.amazonaws.com:17228/em-video-proxy/api/newIp'
    res = requests.get(url=url).json()
    ip_port = res['ip']
    return ip_port


# 下载图片，需要传入图片的绝对路径
def download_pic(url):
    # 获得文件的字节数组
    try:
        request = requests.get(url)
    except Exception as e:
        return None
    if request.status_code == 200:
        bytes = request.content
    else:
        logging.error('url is invalid')
        return None
    request = requests.post(UPLOAD_URL, data=bytes)
    if request.status_code == 200:
        return request.content.decode()
    else:
        logging.error('download failed')
        return None
